package jalus.service;

import jalus.utils.PojoHandler;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.UserFeed;

public class PicasaService {

	private PicasawebService picasaService;
	private static PicasaService instance = new PicasaService();
	private Cache cache;
	
	private static final String picasaPrefix = "http://picasaweb.google.com/data/feed/api/user/";
	
	private UserFeed getUserFeed(){
		UserFeed userFeed;
		try {
			URL feedUrl = new URL(picasaPrefix + ConfigureService.getInstance().getPicasaAlbumName() + "?kind=album");
			userFeed = picasaService.getFeed(feedUrl, UserFeed.class);
		} catch (Exception e) {
			userFeed = null;
		}
		return userFeed;
	}
	
	@SuppressWarnings("unchecked")
	private PicasaService(){
		picasaService = new PicasawebService("xine_res-jalus-1");
		//picasaService.setUserCredentials(ConfigureManager.getInstance().getGoogleAccount(), 
		//			ConfigureManager.getInstance().getPassword());
		Map props = new HashMap();
		// something wrong with EXPIRATION delta
		// bug accepted. See http://code.google.com/p/googleappengine/issues/detail?id=1284
		//props.put(GCacheFactory.EXPIRATION_DELTA, 60 * 60);
		try {
			CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
			cache = cacheFactory.createCache(props);
		} catch (CacheException e) {
			cache = null;
		}
	}

	public static PicasaService getInstance(){
		return instance;
	}
	
	/**
	 * 获得所有相册列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<jalus.pojo.AlbumEntry> getAblumList(){
		String key = "getAblumList";
		if(!cache.containsKey(key)){
			UserFeed userFeed = getUserFeed();
			if(userFeed == null){
				return null;
			}
			cache.put(key, PojoHandler.copyMToPList(userFeed.getAlbumEntries()));
		}
		return (List<jalus.pojo.AlbumEntry>) cache.get(key);
	}
	
	/**
	 * 获取相册中的照片列表
	 * @param albumId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public jalus.pojo.AlbumFeed getPhotoListByAlbum(String albumName){
		String key = "getPhotoListByAlbum@" + albumName;
		jalus.pojo.AlbumFeed albumFeed;
		if(!cache.containsKey(key)){
			URL feedUrl;
			try {
				feedUrl = new URL(picasaPrefix + ConfigureService.getInstance().getPicasaAlbumName() 
						+ "/album/" +albumName + "?kind=photo");
				cache.put(key, PojoHandler.copyMToP(picasaService.getFeed(feedUrl, AlbumFeed.class)));
			} catch (Exception e) {
				return null;
			}
		}
		albumFeed = (jalus.pojo.AlbumFeed) cache.get(key);
		return albumFeed;
	}
	
	/**
	 * 获取指定相册中的指定照片
	 * @param albumName
	 * @param photoId
	 * @return
	 */
	public jalus.pojo.PhotoEntry getPhotoByAlbumAndId(String albumName, int photoId){
		List<jalus.pojo.PhotoEntry> photoList = getPhotoListByAlbum(albumName).getPhotoEntries();
		return photoList.get(photoId);
	}

	public void clearCache() {
		cache.clear();
	}


	
}
